package io.github.junxworks.qt.modules.mm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.sys.annotations.EpLog;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.mm.dto.MmModelCondition;
import io.github.junxworks.qt.modules.mm.dto.MmModelDto;
import io.github.junxworks.qt.modules.mm.dto.ModelExecDto;
import io.github.junxworks.qt.modules.mm.engine.Engine;
import io.github.junxworks.qt.modules.mm.engine.ExeContext;
import io.github.junxworks.qt.modules.mm.service.MmModelService;
import io.github.junxworks.qt.modules.tushare.service.TradeInfoService;

/**
 * mm_model Entity Controller Class
 *
 * @since Mar 23, 2022 3:08:04 PM Generated by JunxEP
 */
@RestController
@RequestMapping("/mm/models")
public class MmModelController {
	@Autowired
	private MmModelService mmModelService;

	@Autowired
	private TradeInfoService tradeInfoService;

	@Autowired
	private Engine engine;

	/**
	 * Query list.
	 *
	 * @param condition the condition
	 * @return the result
	 */
	@GetMapping()
	public Result queryMmModelList(MmModelCondition condition) {
		//查询列表数据
		PageUtils.setPage(condition);
		return Result.ok(new PageInfo<>(mmModelService.queryList(condition)));
	}

	/**
	 * Save .
	 *
	 * @param dto the dto
	 * @return the result
	 */
	@EpLog("模型-模型管理-保存模型")
	@PostMapping()
	public Result saveMmModel(@RequestBody MmModelDto dto) {
		mmModelService.save(dto);
		return Result.ok();
	}

	/**
	 * Query entity by id.
	 *
	 * @param id the id
	 * @return the result
	 */
	@GetMapping("/{id}")
	public Result queryMmModelEntityById(@PathVariable("id") Long id) {
		return Result.ok(mmModelService.queryMmModelById(id));
	}

	@GetMapping("/{id}/expressions")
	public Result queryExpressionsByModelId(@PathVariable("id") Long id) {
		return Result.ok(mmModelService.queryExpressionsByModelId(id));
	}

	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the result
	 */
	@EpLog("模型-模型管理-删除模型")
	@DeleteMapping("/{id}")
	public Result deleteMmModel(@PathVariable("id") Long id) {
		MmModelDto dto = new MmModelDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		mmModelService.save(dto);
		return Result.ok();
	}

	@GetMapping("/trade-dates/{begin}/{end}")
	public Result queryAvailableTradeDtes(@PathVariable("begin") String start, @PathVariable("end") String end) {

		return Result.ok();
	}

	@PostMapping("/executions")
	public Result execute(@RequestBody ModelExecDto dto) throws Exception {
		ExeContext ctx = new ExeContext();
		if (StringUtils.notNull(dto.getUid())) {
			ctx.setUID(dto.getUid());
		}
		String tradeDate = dto.getTradeDate();
		if (dto.isSkipWeekend() && tradeInfoService.isWeekend(tradeDate)) {
			return Result.ok(ctx.getUID());
		}
		ctx.setExecDate(dto.getExecDate());
		ctx.setModelNos(dto.getModelNos());
		ctx.setStockCodes(dto.getStockCodes());
		ctx.setTradeDate(tradeDate);
		ctx.setOnlyStat(dto.getOnlyStat());
		ctx.setStatNames(dto.getStatNames());
		ctx.setIndustry(dto.getIndustry());
		ctx = engine.execute(ctx);
		if (ctx.isInitialized()) {
			return Result.ok(ctx.getUID());
		} else {
			return Result.error("执行上下文初始化失败");
		}
	}

	@GetMapping("/status/{UID}")
	public Result status(@PathVariable("UID") String uid) throws Exception {
		ExeContext ctx = engine.getExeCtxCache().getOrDefault(uid, null);
		return Result.ok(ctx == null ? true : ctx.done());
	}

}